
import { LogicServer } from "../../LogicServer";
import { EnumCacheGroup } from "../../../../common/enum/EnumCacheGroup";
import { UserData } from "../../../../common/model/UserData";
import { SC_MsgList } from "../../../../common/protocols/Proto_MsgList";
import { CacheComponent } from "../../../../framework/service/Components/CacheComponent";
import { IMsg } from "../../../../framework/service/Components/WorkOrderInfo";
import { Tools } from "../../../../framework/utils/Tools";

export class UserEntity {
    private _data: UserData = null;

    private _connection: any;

    private _isLogined: boolean = false;

    private _onlineTime: number = 0;

    private _isRobot: boolean = false;

    private _lastSaveTime: number = Tools.getServerTime();

    constructor() {
        this._onlineTime = Tools.getServerTime();
    }
    public send(msg: IMsg): void {
        // this._cacheMsgs.push(msg);
        // this.sendToClient();
        this._connection.sendUTF(JSON.stringify(msg));
    }




    public setData(data: UserData): void {
        this._data = data;

    }

    public get data(): UserData {
        return this._data;
    }
    public set coin(val: number) {
        if (val < 0) {
            val = 0;
        }

        if (val == this._data.gameData.coins) {
            return;
        }

        // const sc: SC_ValChanged = new SC_ValChanged();
        // sc.valType = EnumValType.coin;
        // sc.value = val;

        this._data.gameData.coins = val;
        //this.send(sc);
        this.save();
    }



    public setConnection(connection): void {
        this._connection = connection;

    }

    public disconnected(): void {
        this._connection = null;
    }



    /**
     * 是否离线了
     *
     * @return {*}  {boolean}
     * @memberof UserEntity
     */
    public isOffline(): boolean {

        if (!this._isLogined) {
            return true;
        }

        if (!this._connection) {
            return true;
        }

        const curTime: number = Tools.getServerTime();
        const delta: number = curTime - this._onlineTime;

        if (delta >= 10) {
            return true;
        }

        return false;
    }

    public updateOnlineTime(): void {
        this._onlineTime = Tools.getServerTime();
    }
    public save(): void {
        const cacheComp: CacheComponent = <CacheComponent>LogicServer.entity.getComponent(CacheComponent)
        cacheComp.set(EnumCacheGroup.USER_DATA, this._data.basic.username, this._data);
        this._lastSaveTime = Tools.getServerTime();
    }

    public set isRobot(val: boolean) {
        this._isRobot = val;
    }

    public get isRobot(): boolean {
        return this._isRobot;
    }

    public update(servertime: number): void {
        
        if(servertime - this._lastSaveTime >= 15){
            //**每15秒钟保存一次数据 */
            this.save();
        }
    }


    public kickoff():void{
        if(this._connection){
            this._connection.close()
            this._connection = null;
        }
    }
}